//
//  dianPuGuanLiViewController.m
//  黑土公社商户端
//
//  Created by 1 on 16/2/3.
//
//

#import "dianPuGuanLiViewController.h"
#import "ImagePickerViewController.h"
#define URL @"http://app.heitugs.com/app_server/app_d_info.aspx?";
@interface dianPuGuanLiViewController ()

@end

@implementation dianPuGuanLiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"店铺管理";
    
    //背景颜色
    self.view.backgroundColor = [UIColor blackColor];
    
    //    1.初始化数据
    self.titles = @[@"商家名称",@"商家号",@"商家地址",@"商家照片",@"商家二维码",@"商家公告",@"营业时间",@"订餐电话",@"是否接受预定"];

    // 2.初始化子视图
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 50) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    self.shangjiaxinxiarray = [[NSArray alloc] init];
    NSString *urlstr = URL;
    NSURL *myurl = [NSURL URLWithString:urlstr];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl];
    //设置表单提交项
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *str = [ user objectForKey:@"1"];
    NSString *str1 = [request responseString];
    str1 = [str substringWithRange:NSMakeRange(36, 11)];
    NSString *str2 = [request responseString];
    str2 = [str substringWithRange:NSMakeRange(62, 2)];
    [request setPostValue:str1 forKey:@"usernum"];
    [request setPostValue:str2 forKey:@"companyid"];
    [request setPostValue:@"l" forKey:@"type"];
    __block NSArray *arr = nil;
    [request setCompletionBlock:^{
        //接收字符串数据
        _shangjiaxinxiarray = [[NSJSONSerialization JSONObjectWithData:request.responseData options:NSUTF8StringEncoding error:nil] retain];
        
        NSLog(@"%@",_shangjiaxinxiarray);
        [_tableView reloadData];
    }];
    [request startAsynchronous];

}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"moreCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer] autorelease];
        // 清空单元格的背景颜色
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView = nil;
        // 设置单元格文本的颜色
        cell.textLabel.textColor = [UIColor blackColor];
        
        // 给单元格创建文本
        UILabel *sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 120 - 20, 12, 120, 20)];
        sizeLabel.textAlignment = NSTextAlignmentRight;
        sizeLabel.font = [UIFont systemFontOfSize:12];
        sizeLabel.textColor = [UIColor blackColor];
        sizeLabel.tag = 100;
        [cell.contentView addSubview:sizeLabel];
        [sizeLabel release];
    }
    // 设置标题
    cell.textLabel.text = _titles[indexPath.row];
//    NSDictionary *dic1 = _shangjiaxinxiarray[indexPath.row];
    // 显示缓存大小的文本
    UILabel *sizeLabel = (UILabel *)[cell.contentView viewWithTag:100];
    if (indexPath.row == 0) {
    sizeLabel.text = @"黑土公社直营店";
    }
    if (indexPath.row == 1) {
    sizeLabel.text = @"123";
    }
    if (indexPath.row == 2) {
    sizeLabel.text = @"哈尔滨";
    }
    if (indexPath.row == 3) {
    sizeLabel.text = @"照片";
    }
    if (indexPath.row == 4) {
    sizeLabel.text = @"二维码";
    }
    if (indexPath.row == 5) {
    sizeLabel.text = @"欢迎品尝";
    }
    if (indexPath.row == 6) {
    sizeLabel.text = @"全天";
    }
    if (indexPath.row == 7) {
    sizeLabel.text = @"1234567890";
    }
    if (indexPath.row == 8) {
    sizeLabel.text = @"是/否";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 8) {
        // 我们点击的是是否接受预定
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"预定" message:@"是否接受预定？" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
        [alert show];
    }
    if (indexPath.row == 3) {
        ImagePickerViewController *imagePicker = [[ImagePickerViewController alloc] init];
        [self.navigationController pushViewController:imagePicker animated:YES];
    }
    
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSLog(@"点击的是确定");
        // 使用SDWebImage内部的方法进行清楚缓存
//        [[SDImageCache sharedImageCache] clearDisk];
        // 刷新表视图
        [_tableView reloadData];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
